import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './plugins/element.js'
import ElementUI from 'element-ui'
import './assets/css/blue-skin5152.css'
import './assets/css/bootstrap5152.css'
import './assets/css/green-skin5152.css'
import './assets/css/main5152.css'
import './assets/css/red-skin5152.css'
import './assets/css/responsive5152.css'
import './assets/css/ali.css'
import Vuex from 'vuex'
import axios from 'axios'
import qs from 'qs'
// 导入时间插件
import moment from 'moment'
// 导入数据图插件
import ECharts from 'vue-echarts'
import 'echarts/lib/chart/bar'
import Vue2Editor from 'vue2-editor'
import 'vue2-editor/dist/vue2-editor.css'
// 导入进度条插件
import NProgress from 'nprogress'
// 导入进度条样式
import 'nprogress/nprogress.css'

Vue.config.productionTip = false
// axios基础url
axios.defaults.baseURL = 'http://localhost:3000'
// 将axios写成vue的原型属性
Vue.prototype.$axios = axios
// 为post请求设置默认请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
// vue2中使用axios，我们请求的参数仍为json类型，是并没有序列化的。我们需要使用querystring解决该问题
Vue.prototype.qs = qs
// 添加一个请求拦截器
axios.interceptors.request.use(config => {
  NProgress.start()
  const luffyJwToken = window.sessionStorage.getItem('luffy_jwt_token')
  config.headers.Authorization = `Bearer ${luffyJwToken}`
  return config
}, err => {
  return Promise.reject(err)
})
// 添加一个响应拦截器
axios.interceptors.response.use(response => {
  NProgress.done()
  if (response.data && response.data.code) {
    if (parseInt(response.data.code) === 401) {
      ElementUI.Notification({
        title: '警告',
        message: '登录过期请重新登录',
        type: 'warning'
      })
      store.dispatch('user/close')// 登录弹窗
      store.dispatch('user/deleteuserinfo')// 清除用户信息
    }
    if (parseInt(response.data.code) === -1) {
      ElementUI.Notification({
        title: '警告',
        message: '请求失败',
        type: 'warning'
      })
    }
  }
  return response
}, error => {
  NProgress.done()
  console.log(error)
  console.dir(error)
  ElementUI.Notification({
    title: '警告',
    message: '服务器连接失败',
    type: 'warning'
  })
  return Promise.reject(error)
})
// 定义全局时间过滤器
Vue.filter('dataFormat', function (dataStr, pattern = 'YYYY-MM-DD HH:mm') {
  return moment(dataStr).format(pattern)
})

Vue.use(ElementUI)
Vue.use(Vuex)
Vue.component('chart', ECharts)
Vue.use(Vue2Editor)

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
